<?php
	class newsControl extends skymvc{
		function __construct(){
			parent::__construct();
		}
		
		public function onDefault(){
			$start=get('per_page','i');
			$limit=24;
			 
			$where=" status<11 AND  siteid=".SITEID;
			$url=APPADMIN."?m=news";
			$order="newsid DESC";
			$keywords=get('keywords','h');
			if($keywords){
				$where.=" AND content like '%".$keywords."%'";
				$url.="&keywords=".urlencode($keywords);
			}
			$is_recommend=get('is_recommend','i');
			if($is_recommend){
				$where.=" AND is_recommend=1 ";
				$url.="&is_recommend=1";
			}
			$orderby=get('orderby');
			if($orderby){
				$url.="&orderby=".$orderby;
				switch($orderby){
					case 1:
							$order=" comment_num DESC,newsid DESC";
						break;
					case 2:
							$order=" spread_num DESC,newsid DESC";
						break;
					case 3:
							$order="love_num DESC,newsid DESC";
						break;
					
				}
			}
			$rscount=true;
			
			$option=array(
				"where"=>$where,
				"start"=>$start,
				"limit"=>$limit,
				"order"=>$order
			);
			$data=M("news")->select($option,$rscount);
			
			if($data){
				foreach($data as $v){
					$uids[]=$v['userid'];
				}
				$us=M("user")->getUserByIds($uids); 
				
				
				foreach($data as $k=>$v){
					if($v['imgsdata']){
							$v['imgsdata']=explode(",",$v['imgsdata']);
						}else{
							$v['imgsdata']="";
					}
					$v['nickname']=$us[$v['userid']]['nickname'];
					$v['user_head']=$us[$v['userid']]['user_head'];		
					 
					$data[$k]=$v;
				}
			}
			$per_page=$start+$limit;
			$per_page=$per_page>=$rscount?0:$per_page;
			$pagelist=$this->pagelist($rscount,$limit,$url);
			$this->smarty->goassign(array(
				"data"=>$data,
				"pagelist"=>$pagelist,
				"per_page"=>$per_page
			));
			$this->smarty->display("news/index.html");
		}
		
		public function onRecommend(){
			$newsid=get('newsid','i');
			$is_recommend=get('is_recommend','i');
			M("news")->update(array("is_recommend"=>$is_recommend),array("newsid"=>$newsid));	
			echo json_encode(array("error"=>0,"message"=>$this->lang['save_success']));	 
		}
		
		public function onDelete(){
		 
			$newsid=get('newsid','i');
			$row=M("news")->selectRow("newsid=".$newsid);
			if($row['siteid']!=SITEID){
				$this->goAll("暂无权限",1);
			}
			M("news")->update(array(
				"status"=>11
			),"newsid=".$newsid);
			//删除推送
			M("news_feeds")->delete("newsid=".$newsid);
			if($row['fromid']){
				M("news")->changenum("spread_num",-1,"newsid=".$row['fromid']);
			}
			$this->goAll("删除成功");
		}
		
		
	}
?>